import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:kkhousekeep/page/home/convenient/repair/repair_home_controller.dart';

import '../../../../widget/rating_bar_widget.dart';

///服务评价对话框
class RatingBarDialog extends StatelessWidget {
  int orderId;
  RepairHomeController controller;
  bool isEvaluated;

  double responseSpeed;
  double serviceAttitude;
  double constructionQuality;

  RatingBarDialog({
    Key? key,
    required this.orderId,
    required this.controller,
    this.isEvaluated = false,
    this.responseSpeed = 0,
    this.serviceAttitude = 0,
    this.constructionQuality = 0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 642.w,
      height: 446.h,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16.w),
      ),
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          ///响应速度 value
          Positioned(
            top: 135.h,
            right: 54.w,
            height: 44.h,
            child: _builderRatingBarWidget(
              onChange: (value) => responseSpeed = value,
              size: responseSpeed,
            ),
          ),

          ///服务质量 value
          Positioned(
            top: 203.h,
            right: 54.w,
            height: 44.h,
            child: _builderRatingBarWidget(
              onChange: (value) => serviceAttitude = value,
              size: serviceAttitude,
            ),
          ),

          ///施工质量 value
          Positioned(
            top: 271.h,
            right: 54.w,
            height: 44.h,
            child: _builderRatingBarWidget(
              onChange: (value) => constructionQuality = value,
              size: constructionQuality,
            ),
          ),

          ///底部按钮
          Positioned(
              left: 0,
              right: 0,
              bottom: 0.h,
              height: 88.h,
              child: _builderBottomWidget(isEvaluated)),

          ///标题栏
          Positioned(
              top: 32.h,
              child: Text(
                "服务评价",
                style: TextStyle(
                  fontSize: 36.sp,
                  color: const Color(0XFF333333),
                ),
              )),
          Positioned(
              left: 84.w,
              top: 130.h,
              child: Text(
                "响应速度",
                style: TextStyle(
                  fontSize: 36.sp,
                  color: const Color(0XFF6D6D6D),
                ),
              )),
          Positioned(
              left: 84.w,
              top: 198.h,
              child: Text(
                "服务态度",
                style: TextStyle(
                  fontSize: 36.sp,
                  color: const Color(0XFF6D6D6D),
                ),
              )),
          Positioned(
              left: 84.w,
              top: 266.h,
              child: Text(
                "施工质量",
                style: TextStyle(
                  fontSize: 36.sp,
                  color: const Color(0XFF6D6D6D),
                ),
              )),

          ///分割线
          Positioned(
              bottom: 88.h,
              left: 0,
              right: 0,
              height: 2.h,
              child: Container(
                color: const Color(0XFFF0F2F7),
              )),
        ],
      ),
    );
  }

  Widget _builderRatingBarWidget({
    ValueChanged? onChange,
    double size = 0,
  }) {
    return RatingBar.builder(
      initialRating: size,
      minRating: 0,
      itemCount: 5,
      itemSize: 44.w,
      ignoreGestures: isEvaluated,
      unratedColor: const Color(0xFFd6d6d6),
      itemPadding: EdgeInsets.symmetric(horizontal: 12.w),
      itemBuilder: (context, _) => ShaderMask(
        shaderCallback: (bounds) {
          return const LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomRight,
            colors: [
              Color(0XFF0A4EF1),
              Color(0XFF7AE0E7),
            ],
          ).createShader(bounds);
        },
        blendMode: BlendMode.srcIn,
        child: const Icon(
          Icons.star,
        ),
      ),
      onRatingUpdate: (rating) {
        onChange?.call(rating);
      },
    );
  }

  Widget _builderBottomWidget(bool isEvaluated) {
    if (isEvaluated) {
      return InkWell(
        onTap: () {
          SmartDialog.dismiss();
        },
        child: Align(
          child: Text(
            "关闭",
            style: TextStyle(fontSize: 34.sp, color: const Color(0XFF999999)),
          ),
        ),
      );
    }
    return Row(
      children: [
        Expanded(
            child: InkWell(
          onTap: () => SmartDialog.dismiss(),
          child: Align(
              child: Text(
            "取消",
            style: TextStyle(
              fontSize: 34.sp,
              color: const Color(0XFF999999),
            ),
          )),
        )),
        Container(
          width: 2.w,
          height: 88.h,
          color: const Color(0XFFD6D6D6),
        ),
        Expanded(
            child: InkWell(
          onTap: () {
            controller.performClickDialogSubmit(
              orderId: orderId,
              responseSpeed: responseSpeed.toInt(),
              serviceAttitude: serviceAttitude.toInt(),
              constructionQuality: constructionQuality.toInt(),
            );
          },
          child: Align(
            child: Text(
              "提交",
              style: TextStyle(
                fontSize: 34.sp,
                color: const Color(0XFF1774E0),
              ),
            ),
          ),
        )),
      ],
    );
  }
}
